package com.yicong.ants.ui.order;

import androidx.databinding.library.baseAdapters.BR;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.cchao.simplelib.ui.fragment.SimpleLazyFragment;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.databinding.CommonRecylerBinding;
import com.yicong.ants.manager.AdapterHelper;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * 二次消费列表
 *
 * @author cchao
 * @version 2019-07-31.
 */
public class AddedProjectOrderListFragment extends SimpleLazyFragment<CommonRecylerBinding> {
    StatefulBindQuickAdapter<com.yicong.ants.bean.order.BillHistoryBean> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.recyler;
    }

    @Override
    public void onFirstUserVisible() {
        initAdapter();
    }

    private void initAdapter() {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        RecyclerView recyclerView = mDataBind.recycler;
        mAdapter = new StatefulBindQuickAdapter<com.yicong.ants.bean.order.BillHistoryBean>(R.layout.bill_history_item) {

            @Override
            protected void convert(DataBindViewHolder helper, com.yicong.ants.bean.order.BillHistoryBean item) {
                helper.getBinding().setVariable(BR.bean, item);
                helper.setTextColor(R.id.order_status, UiHelper.getColor(item.getOrder_status() <= 10 ? R.color.text_333 : R.color.red_primary));
            }

            @Override
            public void loadPageData(int page) {
                final Map<String, String> params = new HashMap<>();
                params.put("page", String.valueOf(page));

                if (page == 1) {
                    setViewState(MultiStateView.VIEW_STATE_LOADING);
                }

                addSubscribe(RetrofitHelper.getApis().getBillHistory(params)
                    .compose(RxHelper.toMain())
                    .subscribe(respBean -> {
                        AdapterHelper.solvePageBean(recyclerView, page, respBean);
                    }, RxHelper.getSwitchableErrorConsumer(this)));
            }
        };

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);

        DividerItemDecoration decoration;
        recyclerView.addItemDecoration(decoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        decoration.setDrawable(UiHelper.getDrawable(R.drawable.divider_home_news));

        mAdapter.setOnItemClickListener((adapter1, view, position) -> {
            //
            Router.turnTo(mContext, BillDetailActivity.class)
                .putExtra(Constants.Extra.JSON, JsonHelper.toJson(mAdapter.getData().get(position)))
                .start();
        });
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.loadPageData(1);
    }

    @Override
    protected void onLoadData() {

    }

}
